package cn.iocoder.yudao.module.reservation.controller.admin.accountbalance;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.reservation.controller.admin.accountbalance.vo.*;
import cn.iocoder.yudao.module.reservation.dal.dataobject.accountbalance.AccountBalanceDO;
import cn.iocoder.yudao.module.reservation.service.accountbalance.AccountBalanceService;

@Tag(name = "管理后台 - 会员余额")
@RestController
@RequestMapping("/reservation/account-balance")
@Validated
public class AccountBalanceController {

    @Resource
    private AccountBalanceService accountBalanceService;

    @PostMapping("/create")
    @Operation(summary = "创建会员余额")
    @PreAuthorize("@ss.hasPermission('reservation:account-balance:create')")
    public CommonResult<Long> createAccountBalance(@Valid @RequestBody AccountBalanceSaveReqVO createReqVO) {
        return success(accountBalanceService.createAccountBalance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员余额")
    @PreAuthorize("@ss.hasPermission('reservation:account-balance:update')")
    public CommonResult<Boolean> updateAccountBalance(@Valid @RequestBody AccountBalanceSaveReqVO updateReqVO) {
        accountBalanceService.updateAccountBalance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员余额")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('reservation:account-balance:delete')")
    public CommonResult<Boolean> deleteAccountBalance(@RequestParam("id") Long id) {
        accountBalanceService.deleteAccountBalance(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员余额")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reservation:account-balance:query')")
    public CommonResult<AccountBalanceRespVO> getAccountBalance(@RequestParam("id") Long id) {
        AccountBalanceDO accountBalance = accountBalanceService.getAccountBalance(id);
        return success(BeanUtils.toBean(accountBalance, AccountBalanceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员余额分页")
    @PreAuthorize("@ss.hasPermission('reservation:account-balance:query')")
    public CommonResult<PageResult<AccountBalanceRespVO>> getAccountBalancePage(@Valid AccountBalancePageReqVO pageReqVO) {
        PageResult<AccountBalanceDO> pageResult = accountBalanceService.getAccountBalancePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccountBalanceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员余额 Excel")
    @PreAuthorize("@ss.hasPermission('reservation:account-balance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAccountBalanceExcel(@Valid AccountBalancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AccountBalanceDO> list = accountBalanceService.getAccountBalancePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员余额.xls", "数据", AccountBalanceRespVO.class,
                        BeanUtils.toBean(list, AccountBalanceRespVO.class));
    }

}